package com.newlink.building.login.utils

import com.newlink.building.common_base.utils.NLog
import com.newlink.building.login.bean.UserAccountInfo

/**
 * Created by xiaodong on 2024/12/24.
 */
object Module_Login_AccountMemoryUtils {

    fun saveAccountInfo(
        userName: String,
        userId: String,
        email: String,
        pwd: String,
        userCategory: Int,
        userInfos: MutableList<UserAccountInfo>,
        mobile: String
    ): MutableList<UserAccountInfo> {
        var userAccountInfoList = userInfos
        userAccountInfoList = if (checkUserAccountEmpty(userInfos)) {
            mutableListOf(
                UserAccountInfo(
                    userName,
                    userId,
                    email,
                    pwd,
                    System.currentTimeMillis(),
                    userCategory,
                    mobile = mobile
                )
            )

        } else {
            val accountList = userAccountInfoList
            if (!checkUserAccountExists(accountList, userName)) {
                //账号不存在则添加
                addAccountInfo(
                    accountList,
                    UserAccountInfo(
                        userName,
                        userId,
                        email,
                        pwd,
                        System.currentTimeMillis(),
                        userCategory,
                        mobile = mobile
                    )
                )
            } else {
                val changePassword =
                    checkIfNeedChangePassword(accountList, userName, pwd)
                NLog.e("[AccountMemoryUtils]  changePassword:$changePassword")
                //账号已存在更新时间戳
                updateAccountInfoTimestamp(accountList, userName)
            }
            //按最近登录的时间进行排序
            accountList.sortByDescending { it.timestamp }
            NLog.e("[AccountMemoryUtils]  sortByDescending userAccountInfoList:$userAccountInfoList")

            accountList
        }

        return userAccountInfoList
    }

    fun addAccountInfo(
        accountList: MutableList<UserAccountInfo>,
        userAccountInfo: UserAccountInfo,
    ) {
//        if (accountList.size >= 5) {
//            accountList.removeAt(accountList.size - 1)
//        }
        accountList.add(0, userAccountInfo)
    }

    fun checkUserAccountEmpty(userInfos: MutableList<UserAccountInfo>): Boolean {
        return userInfos.size == 1 && userInfos[0].timestamp == 0L
    }

    fun checkUserAccountExists(
        accountList: MutableList<UserAccountInfo>,
        username: String,
    ): Boolean {
        var isExist = false
        for (item in accountList) {
            if (username == item.username) {
                isExist = true
            }
        }
        return isExist
    }

    fun checkIfNeedChangePassword(
        accountList: MutableList<UserAccountInfo>,
        username: String,
        password: String,
    ): Boolean {
        var isExist = false
        for (item in accountList) {
            if (username == item.username && password != item.pwd) {
                item.pwd = password
                isExist = true
            }
        }
        return isExist
    }

    private fun updateAccountInfoTimestamp(
        accountList: MutableList<UserAccountInfo>,
        account: String,
        timestamp: Long = System.currentTimeMillis(),
    ) {
        for (item in accountList) {
            if (item.username == account) {
                item.timestamp = timestamp
            }
        }
    }
}